#!/bin/bash
taskNo=general
echo "Please waiting about 10s...."
HostName=`hostname`
today=`date +%Y%m%d%H%M%S`
host1=`grep -w "${HostName}" /etc/hosts | grep -v "#" | awk '$1~/\./{print $1,$2}' | sed -n  '1p' | awk '{print $1}' |  wc | awk '{print $1}'`
host=`grep -w "${HostName}" /etc/hosts | grep -v "#" | awk '$1~/\./{print $1,$2}' | sed -n  '1p' | awk '{print $1}'`
host2=`/sbin/ifconfig | grep 'inet addr' | sed -n '1p' | sed 's/^.*addr://g' | sed 's/  Bcast.*$//g'`
host3=`grep -w "${HostName}" /etc/hosts | grep -v "#" |grep -v "127"| awk '$1~/\./{print $1,$2}' | sed -n  '1p' | awk '{print $1}'|  wc | awk '{print $1}'`
if [ "$host1" -eq 0 ];then
  Host_ip=$host2
elif [ "$host3" -eq 0 ];then
  Host_ip=$host2
else
  Host_ip=$host
fi
cd /tmp
rm -rf ${taskNo}_Redhat_${Host_ip}_${HostName}_*_rwjczj
dirName=${taskNo}_Redhat_${Host_ip}_${HostName}_${today}_rwjczj
mkdir $dirName
directoryName=/tmp/$dirName
cd $directoryName
touch ${Host_ip}_${HostName}_${today}_rwjczj.log
touch ${Host_ip}_version.conf
touch ${Host_ip}_core.conf
touch ${Host_ip}_filesys.conf
touch ${Host_ip}_filesysspace.conf
touch ${Host_ip}_lancheck.conf
touch ${Host_ip}_softjar.conf
touch ${Host_ip}_basepath.conf
touch ${Host_ip}_timecheck.conf
touch ${Host_ip}_kdump.conf
touch ${Host_ip}_swapcheck.conf
touch ${Host_ip}_hostscheck.conf
touch ${Host_ip}_sendmail.conf
touch ${Host_ip}_snmpcheck.conf
touch ${Host_ip}_sshcheck.conf
touch ${Host_ip}_rootcheck.conf
touch ${Host_ip}_systemlog.conf
touch ${Host_ip}_cpucheck.conf
touch ${Host_ip}_memcheck.conf
touch ${Host_ip}_shellshock.conf
touch ${Host_ip}_ntpstarted.conf
touch ${Host_ip}_ntpaddress.conf
touch ${Host_ip}_offset.conf
touch ${Host_ip}_hostmonitor.conf
touch ${Host_ip}_syslog.conf
touch ${Host_ip}_accountlist.conf
touch ${Host_ip}_divercheck.conf

version=${Host_ip}_version.conf
core=${Host_ip}_core.conf
filesys=${Host_ip}_filesys.conf
filesysspace=${Host_ip}_filesysspace.conf
lancheck=${Host_ip}_lancheck.conf
softjar=${Host_ip}_softjar.conf
basepath=${Host_ip}_basepath.conf
timecheck=${Host_ip}_timecheck.conf
kdump=${Host_ip}_kdump.conf
swapcheck=${Host_ip}_swapcheck.conf
hostscheck=${Host_ip}_hostscheck.conf
sendmail=${Host_ip}_sendmail.conf
snmpcheck=${Host_ip}_snmpcheck.conf
sshcheck=${Host_ip}_sshcheck.conf
rootcheck=${Host_ip}_rootcheck.conf
systemlog=${Host_ip}_systemlog.conf
cpucheck=${Host_ip}_cpucheck.conf
memcheck=${Host_ip}_memcheck.conf
shellshock=${Host_ip}_shellshock.conf
ntpstarted=${Host_ip}_ntpstarted.conf
ntpaddress=${Host_ip}_ntpaddress.conf
offset=${Host_ip}_offset.conf
hostmonitor=${Host_ip}_hostmonitor.conf
syslog=${Host_ip}_syslog.conf
accountlist=${Host_ip}_accountlist.conf
divercheck=${Host_ip}_divercheck.conf

FileName=${Host_ip}_${HostName}_${today}_rwjczj.log
Aujp="null"

redVers=`cat /etc/redhat-release | grep -i -E "Red|Centos" | sed -n '1p'`
echo -e "${redVers}\r">${FileName}

#equNum
equNum=`dmidecode | grep -A 8 "System Information"| grep "Product Name"| cut -d ':' -f2| sed 's/^.//g'`
equNum_Num=`echo $equNum | wc -w 2>/dev/null`
if [ $equNum_Num -ne 0 ];then
echo -e "equNum_check_redhat\t\t""equNum_redhat:equal#@${equNum}\t\t""${Aujp} \r">>${FileName}
else
echo -e "equNum_check_redhat\t\t""equNum_redhat:equal#@${Aujp}\t\t""${Aujp} \r">>${FileName}
fi

#equSerial
equSerial=`dmidecode | grep -A 8 "System Information"| grep "Serial Number"| cut -d ':' -f2| sed 's/^.//g'`
equSerial_Num=`echo $equSerial | wc -w 2>/dev/null`
if [ $equSerial_Num -ne 0 ];then
echo -e "equSerial_check_redhat\t\t""equSerial_redhat:equal#@${equSerial}\t\t""${Aujp} \r">>${FileName}
else
echo -e "equSerial_check_redhat\t\t""equSerial_redhat:equal#@${Aujp}\t\t""${Aujp} \r">>${FileName}
fi

#version
echo -e "#cat /etc/redhat-release | awk '{print $1$2$7}'">${version}
cat /etc/redhat-release | awk '{print $1$2$7}' >>${version}
echo -e "version_check_redhat\t\t""version_redhat:equal#@${version}\t\t""${version} \r">>${FileName}

#inDisk
echo -e "inDisk_check_redhat\t\t""inDisk_redhat:equal#@${Aujp}\t\t""${Aujp} \r">>${FileName}

#core
echo -e "#uname -a">${core}
uname -a >>${core}
echo -e "core_check_redhat\t\t""coretype_redhat:equal#@${core}\t\t""${core} \r">>${FileName}

#filesys
echo -e "#df -hlT">${filesys}
df -hlT>>${filesys}
echo -e "file_sys_check_redhat\t\t""system_redhat:equal#@${filesys}\t\t""${filesys} \r">>${FileName}

#filesysspace
echo -e "#df -k">${filesysspace}
df -k >>${filesysspace}
echo -e "fileSysSpace_check_redhat\t\t""fileSysSpace_redhat:equal#@${filesysspace}\t\t""${filesysspace} \r">>${FileName}

#lancheck
echo -e "#cat /etc/sysconfig/i18n">${lancheck}
cat /etc/sysconfig/i18n >>${lancheck}
echo -e "lan_check_redhat\t\t""lan_redhat:equal#@${lancheck}\t\t""${lancheck} \r">>${FileName}

#softjar
echo -e "#rpm -qa | egrep 'bind-utils|ethtool|expect|firefox|iptraf|lftp|gcc|gcc-c++|gcc-gfortran|logwatch|net-snmp|python|sysstat|system-config-kdump|tcpdump|vsftpd|xinetd|compat-libstdc++-33|compat-libstdc++-296'">${softjar}
rpm -qa |egrep 'bind-utils|ethtool|expect|firefox|iptraf|lftp|gcc|gcc-c++|gcc-gfortran|logwatch|net-snmp|python|sysstat|system-config-kdump|tcpdump|vsftpd|xinetd|compat-libstdc++-33|compat-libstdc++-296'>>${softjar}
echo -e "softjar_check_redhat\t\t""softjar_redhat:equal#@${softjar}\t\t""${softjar} \r">>${FileName}

#basepath
echo -e "#uname -r">${basepath}
uname -r >>${basepath}
echo -e "basepatch_check_redhat\t\t""basepatch_redhat:equal#@${basepath}\t\t""${basepath} \r">>${FileName}

#timecheck
echo -e "#cat /etc/sysconfig/clock | grep -v '#' | grep -v ^$">${timecheck}
cat /etc/sysconfig/clock | grep -v '#' | grep -v ^$>>${timecheck}
echo -e "time_check_redhat\t\t""time_redhat:equal#@${timecheck}\t\t""${timecheck} \r">>${FileName}

#kdump
echo -e "#service kdump status">${kdump}
service kdump status >>${kdump} 2>&1
echo -e " " >>${kdump}
echo -e "------------------------------------------------">>${kdump}
echo -e "#LANG="" chkconfig --list kdump">>${kdump}
LANG="" chkconfig --list kdump >>${kdump} 2>&1
echo -e "kdump_check_redhat\t\t""kdump_redhat:equal#@${kdump}\t\t""${kdump} \r">>${FileName}


#swapcheck
echo -e "#free -m">${swapcheck}
free -m >>${swapcheck}
echo -e "swap_check_redhat\t\t""swap_redhat:equal#@${swapcheck}\t\t""${swapcheck} \r">>${FileName}

#hostname
echo -e "hostname_check_redhat\t\t""hostname_redhat:equal#@${HostName}\t\t""${Aujp} \r">>${FileName}

#netParmter
Network_eth=`/sbin/ifconfig | cut -d ' ' -f 1 | grep -v '^$' | grep eth* | sed -n '1p'`
netParmter=`ethtool ${Network_eth} 2>/dev/null | grep -i  "Duplex" | sed 's/^[ \t]*//g'`
NetNum=`echo $netParmter | wc -w`
if [ $NetNum -ne 0 ];then
echo -e "netParamter_check_redhat\t\t""netParamter_redhat:equal#@${netParmter}\t\t""${Aujp} \r">>${FileName}
else
echo -e "netParamter_check_redhat\t\t""netParamter_redhat:equal#@${Aujp}\t\t""${Aujp} \r">>${FileName}
fi

#hostscheck
echo -e "#hostname ">${hostscheck}
hostname >>${hostscheck}
echo -e " ">>${hostscheck}
echo -e "------------------------------------">>${hostscheck}
echo -e "#cat /etc/hosts">>${hostscheck}
cat /etc/hosts >>${hostscheck}
echo -e "hosts_check_redhat\t\t""hosts_redhat:equal#@${hostscheck}\t\t""${hostscheck} \r">>${FileName}

#sendmail
echo -e "#/etc/init.d/sendmail status">${sendmail}
/etc/init.d/sendmail status >>${sendmail} 2>&1
echo -e "sendmail_check_redhat\t\t""sendmail_server_redhat:equal#@${sendmail}\t\t""${sendmail} \r">>${FileName}

#snmpcheck
if [ -f /etc/snmpd.conf ];then
echo -e "#cat /etc/snmpd.conf">${snmpcheck}
cat /etc/snmpd.conf | grep -v "^#" | grep -v "^$" >>${snmpcheck}
echo -e "snmp_check_redhat\t\t""snmp_redhat:equal#@${snmpcheck}\t\t""${snmpcheck} \r">>${FileName}
elif [ -f /etc/snmp/snmpd.conf ];then
echo -e "#cat /etc/snmp/snmpd.conf">${snmpcheck}
cat /etc/snmp/snmpd.conf|grep -v "^#"| grep -v "^$" >>${snmpcheck}
echo -e "snmp_check_redhat\t\t""snmp_redhat:equal#@${snmpcheck}\t\t""${snmpcheck} \r">>${FileName}
else
echo -e "snmp_check_redhat\t\t""snmp_redhat:equal#@${Aujp}\t\t""${Aujp}">>${FileName}
fi

#sshcheck
echo -e "#rpm -qa | grep ssh">${sshcheck}
rpm -qa | grep ssh >>${sshcheck}
echo -e " ">>${sshcheck}
echo -e "--------------------------------">>${sshcheck}
echo -e "#netstat -lntp | grep -w [:]23">>${sshcheck}
netstat -lntp | grep -w [:]23 >>${sshcheck}
echo -e " ">>${sshcheck}
echo -e "---------------------------------">>${sshcheck}
echo -e "#netstat -lntp |grep -w [:]22">>${sshcheck}
netstat -lntp |grep -w [:]22 >>${sshcheck}
echo -e "ssh_check_redhat\t\t""ssh_redhat:equal#@${sshcheck}\t\t""${sshcheck} \r">>${FileName}


#rootcheck
addfile1=`find /etc -name "sshd_config"`
addfile2=`find /usr -name "sshd_config"`
if [ -f "$addfile1" ];then
echo -e "#cat $addfile1 |grep -v "#"|grep "PermitRootLogin no"">${rootcheck}
cat $addfile1 |grep -v "#"|grep "PermitRootLogin no">>${rootcheck}
echo -e "root_check_redhat\t\t""root_redhat:equal#@${rootcheck}\t\t""${rootcheck} \r">>${FileName}
else
echo -e "#cat $addfile2 |grep -v "#"|grep "PermitRootLogin no"">${rootcheck}
cat $addfile2 |grep -v "#"|grep "PermitRootLogin no">>${rootcheck}
echo -e "root_check_redhat\t\t""root_redhat:equal#@${rootcheck}\t\t""${rootcheck} \r">>${FileName}
fi


#systemlog
echo -e "#tail -n 1000 /var/log/messages">${systemlog}
tail -n 1000 /var/log/messages >>${systemlog}
echo -e "systemLog_check_redhat\t\t""systemLog_redhat:equal#@${systemlog}\t\t""${systemlog} \r">>${FileName}

#cpucheck
echo -e "#top -b -n 1">${cpucheck}
top -b -n 1 >>${cpucheck}
echo -e " ">>${cpucheck}
echo -e "-----------------------------------">>${cpucheck}
echo -e "#mpstat -P ALL ">>${cpucheck}
mpstat -P ALL >>${cpucheck}
echo -e "cpu_check_redhat \t\t""cpu_redhat:equal#@${cpucheck}\t\t""${cpucheck}\t\t">>${FileName}
 
#memcheck
echo -e "#free -m">${memcheck}
free -m >>${memcheck}
echo -e "mem_check_redhat\t\t""mem_redhat:equal#@${memcheck}\t\t""${memcheck} \r">>${FileName}

#shellshock
echo -e "#cd /tmp; rm -f /tmp/echo; env 'x=() { (a)=>\' bash -c "echo date" >>$directoryName/${shellshock}  2>&1 ;cd $directoryName; cat /tmp/echo >>${shellshock} 2>>${shellshock}">${shellshock}
cd /tmp; rm -f /tmp/echo; env 'x=() { (a)=>\' bash -c "echo date" >>$directoryName/${shellshock}  2>&1 ;cd $directoryName; cat /tmp/echo >>${shellshock} 2>>${shellshock}
echo -e "shell_shock_redhat\t\t""shell_redhat:equal#@${shellshock}\t\t""${shellshock} \r">>${FileName}




#ntpstarted
echo -e "#service ntpd status">${ntpstarted}
LANG="" service ntpd status >>${ntpstarted} 2>&1
echo -e " ">>${ntpstarted}
echo -e "-----------------------------------------">>${ntpstarted}
echo -e "#LANG="" chkconfig --list ntpd">>${ntpstarted}
LANG="" chkconfig --list ntpd >>${ntpstarted} 2>&1
echo -e "ntpStarted_check_redhat\t\t""ntpStarted_redhat:equal#@${ntpstarted}\t\t""${ntpstarted} \r">>${FileName}

#ntpaddress
echo -e "#ntpq -p">${ntpaddress}
ntpq -p >>${ntpaddress} 2>&1
echo -e "ntpAddress_check_redhat\t\t""ntpAddress_redhat:equal#@${ntpaddress}\t\t""${ntpaddress} \r">>${FileName}

#offset
echo -e "#ntpq -p">${offset}
ntpq -p >>${offset} 2>&1
echo -e "offset_check_redhat\t\t""offset_redhat:equal#@${offset}\t\t""${offset} \r">>${FileName}

#hostmonitor
echo -e "#ps -ef | grep Patrol">${hostmonitor}
ps -ef | grep Patrol >>${hostmonitor}
echo -e "hostMonitor_check_redhat\t\t""hostMonitor_redhat:equal#@${hostmonitor}\t\t""${hostmonitor} \r">>${FileName}

#syslog
if [ -f /etc/rsyslog.conf ];then
echo -e "#cat /etc/rsyslog.conf ">${syslog}
cat /etc/rsyslog.conf | grep -v '^#' |grep -v '^$'>>${syslog} 2>&1
echo -e "syslog_check_redhat\t\t""syslog_redhat:equal#@${syslog}\t\t""${syslog} \r">>${FileName}
else
echo -e "#cat /etc/syslog.conf ">${syslog}
cat /etc/syslog.conf |grep -v '^#'|grep -v '^$' >>${syslog} 2>&1
echo -e "syslog_check_redhat\t\t""syslog_redhat:equal#@${syslog}\t\t""${syslog} \r">>${FileName}
fi

#accountlist
echo -e "#cat /etc/passwd ">${accountlist}
cat /etc/passwd >>${accountlist}
echo -e " ">>${accountlist}
echo -e "----------------------------------">>${accountlist}
echo -e "#cat /etc/shadow">>${accountlist}
cat /etc/shadow >>${accountlist}
echo -e "operateAccount_check_redhat\t\t""operateAccount_redhat:equal#@file\t\t""${accountlist} \r">>${FileName}

#divercheck
hostsize=`dmidecode -s system-product-name |awk '{print $3}'`
hardwareHP=`dmidecode  -s system-Manufacturer|sed 's/^ *//'|awk '{print $1}'`
if [ "$hardwareHP" == "HP" ];then
if [[ "$hostsize" == "G7" || "$hostsize" == "G8" ]];then
echo -e "#uname -r">${divercheck}
uname -r >>${divercheck}
echo -e "------------------------------------------------">>${divercheck}
echo -e "#rpm -q be2net|grep -v 'xen' |grep -v 'src'">>${divercheck}
rpm -q be2net |grep -v 'xen' |grep -v 'src'>>${divercheck}
echo -e "driver_check_redhat\t\t""driver_redhat:equal#@${divercheck}\t\t""${divercheck} \r">>${FileName}
else 
echo -e "driver_check_redhat\t\t""driver_redhat:equal#@${Aujp}\t\t""${Aujp} \r">>${FileName}
fi
fi